/*
Philip Dilse
dilsep
Assignment 4

unit test for salvager card
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "dominion.h"
#include "dominion_helpers.h"

#define NUM_TESTS 30

int main() {
    struct gameState *gs = newGame();
    int player = 0;
    int numBuys, numCoins, numTest, i, handCount;
    int salvagerPos;
    int choice1, choice1Cost;
    int actualReturn;

    // set up random number gen
    srand(time(NULL));
    
    gs->whoseTurn = player;

    for (numTest = 0; numTest < NUM_TESTS; numTest++) {
        handCount = (rand() % 10) + 1; // 1-10
        gs->handCount[player] = handCount;

        // randomly select hand cards
        for (i = 0; i < handCount; i++) {
            gs->hand[player][i] = rand() % (treasure_map + 1);
        }

        // swap salvager card into random spot
        salvagerPos = rand() % handCount;
        gs->hand[player][salvagerPos] = salvager;

        // randomly select choice1 from 0 to handCount - 1
        choice1 = rand() % handCount;

        // cost of choice1
        choice1Cost = getCost(gs->hand[player][choice1]);

        // set discard to have 5 curse cards
        gs->discardCount[player] = 5;

        for (i = 0; i < gs->discardCount[player]; i++) {
            gs->discard[player][i] = curse;
        }

        // set previous num buys to 1, 2, or 3
        numBuys = (rand() % 3) + 1;
        gs->numBuys = numBuys;

        // set previous num coins to 0, 1, or 2
        numCoins = rand() % 3;
        gs->coins = numCoins;
        
        actualReturn = cardEffect(salvager, choice1, 0, 0, gs, salvagerPos, &i);

        // check that numBuys is one more than before
        if (numBuys != gs->numBuys - 1) {
            printf("salvager error: bad numBuys; before=%d after=%d\n", numBuys, gs->numBuys);
        }

        // check that coins is correct
        if (choice1 && numCoins != gs->coins - choice1Cost) {
            printf("salvager error: bad coins; choice1=%d before=%d after=%d\n", choice1, numCoins, gs->coins);
        } else if (!choice1 && numCoins != gs->coins) {
            printf("salvager error: bad coins; choice1=%d before=%d after=%d\n", choice1, numCoins, gs->coins);
        }

        // check that hand is empty if handcount was 1
        if (handCount == 1 && gs->handCount[player] != 0) {
            printf("salvager error: bad hand count; before=%d after=%d\n", handCount, gs->handCount[player]);
        }

        // check that -1 was returned if choice1 == salvagerPos and handCount > 1
        if (choice1 == salvagerPos && handCount > 1 && actualReturn != -1) {
            printf("salvager error: can't pick salvager card for choice1 if another card available\n");
        }

        // check that handCount is two less than before cardEffect() if more than two cards to start with
        if (handCount > 1 && choice1 != salvagerPos && handCount - 2 != gs->handCount[player]) {
            printf("salvager error: bad hand count; choice1=%d before=%d after=%d\n", choice1, handCount, gs->handCount[player]);
        }        

        // check that trashed card not in discard pile
        for (i = 0; i < gs->discardCount[player]; i++) {
            if (gs->discard[player][i] != curse) {
                printf("salvager error: card %d in discard, should be in trash\n", gs->discard[player][i]);
            }
        }

    }

    free(gs);
    return 0;
}
